﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using LowLevelGraphics;
using LowLevelGraphics.Filter;

using ImageRecognition2.Forms;

namespace ImageRecognition2.Action
{
    /// <summary>
    /// Action Variable Threshold
    /// </summary>
    public class ActionVariableThreshold : AbstractCommand
    {
        protected BaseImageFilter m_Filter = null;
        protected FormVariableThreshold formFilter = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="ActionThreshold"/> class.
        /// </summary>
        public ActionVariableThreshold()
        {
            m_Filter = new VariableThreshold();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ActionThreshold"/> class.
        /// </summary>
        /// <param name="_filter">The _filter.</param>
        public ActionVariableThreshold(BaseImageFilter _filter)
        {
            m_Filter = _filter;
        }

        /// <summary>
        /// Gets or sets the filter.
        /// </summary>
        /// <value>The filter.</value>
        public BaseImageFilter Filter
        {
            get { return m_Filter; }
            set { m_Filter = value; }
        }

        /// <summary>
        /// Runs the specified sender.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public override void Run(object sender, EventArgs e)
        {
            //--------------------------------------------------------
            formFilter = new FormVariableThreshold((Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage.Clone(), 0, 255);
            formFilter.trackbRed.Scroll += new EventHandler(trackb_Scroll);
            formFilter.trackbGreen.Scroll += new EventHandler(trackb_Scroll);
            formFilter.trackbBlue.Scroll += new EventHandler(trackb_Scroll);
            if (formFilter.ShowDialog() == DialogResult.OK)
            {
                Bitmap bitmap = (Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage;

                m_Filter = new VariableThreshold(formFilter.chkbKeepColors.Checked, formFilter.trackbRed.Value, formFilter.trackbGreen.Value, formFilter.trackbBlue.Value, string.Empty);
                UnsafeBitmap unsafeBitmap = m_Filter.Execute(bitmap);
                unsafeBitmap.Dispose();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void trackb_Scroll(object sender, EventArgs e)
        {
            VariableThreshold filter = (VariableThreshold)m_Filter;
            filter.R = formFilter.trackbRed.Value;
            filter.G = formFilter.trackbGreen.Value;
            filter.B = formFilter.trackbBlue.Value;
            filter.KeepColors = formFilter.chkbKeepColors.Checked;
            filter.Formula = formFilter.textBox1.Text;
            Execute(formFilter);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="formFilter"></param>
        private void Execute(FormVariableThreshold formFilter)
        {
            Bitmap formFilterpictureBox1BackgroundImageClone = (Bitmap)formFilter.imgOriginal.Clone();
            BaseImageFilter newThreshold = new VariableThreshold(formFilter.chkbKeepColors.Checked, formFilter.trackbRed.Value, formFilter.trackbGreen.Value, formFilter.trackbGreen.Value, formFilter.textBox1.Text);
            newThreshold.ProcessingMode = PROCESSING_MODE.Color;
            newThreshold.Execute(formFilterpictureBox1BackgroundImageClone).Dispose();
            formFilter.pictureBox1.BackgroundImage = formFilterpictureBox1BackgroundImageClone;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class VariableThreshold : BaseForeBackgroundColorFilter
    {
        protected int m_nR = 127;
        protected int m_nG = 127;
        protected int m_nB = 127;
        protected string m_sFormula = string.Empty;
        protected bool m_bKeepColors = false;

        public VariableThreshold()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_nR"></param>
        /// <param name="_nG"></param>
        /// <param name="_nB"></param>
        /// <param name="_sFormula"></param>
        public VariableThreshold(bool _bKeepColors, int _nR, int _nG, int _nB, string _sFormula)
        {
            m_bKeepColors = _bKeepColors;
            m_nR = _nR;
            m_nG = _nG;
            m_nB = _nB;
            m_sFormula = _sFormula;
        }

        public int R
        {
            get { return m_nR; }
            set { m_nR = value; }
        }

        public int G
        {
            get { return m_nG; }
            set { m_nG = value; }
        }

        public int B
        {
            get { return m_nB; }
            set { m_nB = value; }
        }

        public string Formula
        {
            get { return m_sFormula; }
            set { m_sFormula = value; }
        }

        public bool KeepColors
        {
            get { return m_bKeepColors; }
            set { m_bKeepColors = value; }
        }

        /// <summary>
        /// Variable Threshold used to clone
        /// </summary>
        /// <param name="_variableThreshold"></param>
        internal VariableThreshold(VariableThreshold _variableThreshold)
        {
            m_nR = _variableThreshold.m_nR;
            m_nG = _variableThreshold.m_nG;
            m_nB = _variableThreshold.m_nB;
            m_sFormula = _variableThreshold.m_sFormula;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;
            int x = 0;
            int y = 0;

            Color color = Color.Transparent;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = _bitmap.GetPixel(x, y);
                    if (m_bKeepColors)
                    {
                        _bitmap.SetPixel(x, y, color.R > m_nR && color.G > m_nG && color.B > m_nB ? color : this.m_ColorBackground);
                    }
                    else
                    {
                        _bitmap.SetPixel(x, y, color.R > m_nR && color.G > m_nG && color.B > m_nB ? this.m_ColorForeground : this.m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Used to clone this instance
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new VariableThreshold(this);
        }
    }
}
